package de.sendorian.util.matcher;


import java.util.regex.Pattern;

import org.hamcrest.Description;
import org.hamcrest.TypeSafeDiagnosingMatcher;

public class RegexMatcher extends TypeSafeDiagnosingMatcher<String> {

	private static Pattern pattern;


	public RegexMatcher(String regex) {
		pattern = Pattern.compile(regex);
	}


	@Override
	public boolean matchesSafely(String string, Description mismatchDescription) {
		if (!pattern.matcher(string).matches()) {
			mismatchDescription.appendText("was ");
			mismatchDescription.appendValue(string);
			return false;
		}
		return true;
	}


	public void describeTo(Description desc) {
		desc.appendText("a string matches ").appendValue(pattern.pattern());

	}
}
